// pages/classification/classification.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  qrcode = require('../../utils/qrcode.js'),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classiflist: [],
    selindex: 0,
    list: [],
    word: '',
    cid:0,
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    let { cid } = options
    that.setData({ cid, selindex:cid}, () => {
      that.getlist()
    })
  },
  inputtap(e) {
    if (e.detail.value == '') {
      that.setData({ list: [], page: 1 }, () => {
        that.getlist()
      })
    }
    that.setData({ word: e.detail.value })
  },
  getlist() {
    wx.showLoading({
      title: '加载中',
    })
    let { list, page, word, cid, classiflist, cateArr } = that.data
    api.searchgoods({ page, word, cid }, res => {
      wx.hideLoading()
      if (res.data.code == 0) {
        let { infoArr, nextPage,cateArr } = res.data.entity.data
        if (infoArr.length > 0) {
          infoArr.forEach(x => {
            list.push(x)
          })
        }
        that.setData({ list, nextPage,classiflist:cateArr })
      }
    })
  },
  seltype(e) {
    let { selindex } = that.data, { id } = e.currentTarget.dataset
    if (selindex == id) return
    that.setData({ selindex: id, cid: id, list: [], page: 1,word:'' }, () => {
      that.getlist()
    })
  },
  search() {
    let { word } = that.data
    if (word == '') {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      })
      return
    }
    that.setData({ list: [], page: 1 }, () => {
      that.getlist()
    })
  },
  todetail(e) {
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../shopdetail/shopdetail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { nextPage, page } = that.data
    if (nextPage == 1) {
      page = page + 1
      that.setData({ page }, () => {
        that.getlist()
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})